package app.config;

import biz.paluch.spinach.DisqueClient;
import biz.paluch.spinach.DisqueURI;
import biz.paluch.spinach.api.DisqueConnection;
import biz.paluch.spinach.api.sync.DisqueCommands;
import com.github.xetorthio.jedisque.Jedisque;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author faith.huan 2018-09-02 21:47
 */

@Slf4j
@Configuration
public class DisqueConfig {

    // @Bean
    public DisqueCommands<String, String> getDisqueCommands() {

        log.info("connecting:{}", System.currentTimeMillis());
        DisqueClient client = DisqueClient.create(DisqueURI.create("47.105.39.155", 50711));
        log.info("client:{}", System.currentTimeMillis());
        DisqueConnection connection = client.connect();
        log.info("connection:{}", System.currentTimeMillis());
        DisqueCommands commands = connection.sync();
        log.info("connected:{}", System.currentTimeMillis());

        return commands;
    }

    /**
     * 获取链接
     * @return
     */
    public static Jedisque getJedisque(int soTimeout) {
        try {
            Jedisque jedisque=  new Jedisque(
                    // 85
                    new URI("disque://47.105.39.155:50711"),
                    // 88
                    new URI("disque://47.104.89.100:50711"),
                    // 83
                    new URI("disque://47.104.254.218:50711")
            );
            jedisque.setSoTimeout(soTimeout);

            log.info("获取连接成功,Host:{},Port:{}",jedisque.getHost(),jedisque.getPort());
            return  jedisque;
        } catch (URISyntaxException e) {
            log.error("创建Jedisque失败!");
            return null;
        }
    }
}
